﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EpiPoker.DataAccess
{
    /// <summary>
    /// Stored Procedures for T_Portofolio Table
    /// </summary>
    public class Portfolio
    {
        #region UpdateDBB

        /// <summary>
        /// Create Portfolio
        /// </summary>
        /// <param name="Portfolio">Portfolio object to create</param>
        /// <returns>true if goes well</returns>
        public static bool CreatePortfolio(long IdUser, Boolean IsVirtual)
        {
            try
            {
                T_Portfolio portfolio = new T_Portfolio();
                fbPokerEntities bdd = new fbPokerEntities();
                portfolio.DateCreation = DateTime.Now;
                portfolio.Amount = 1000;
                portfolio.IsActive = true;
                portfolio.IdUser = IdUser;

                if (IsVirtual)
                    portfolio.IdPortfolioStatus = bdd.T_PortfolioStatus.Where(x => x.Status == "Virtual").FirstOrDefault().IdPortfolioStatus;
                else
                    portfolio.IdPortfolioStatus = bdd.T_PortfolioStatus.Where(x => x.Status == "Real").FirstOrDefault().IdPortfolioStatus;

                bdd.AddToT_Portfolio(portfolio);
                bdd.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                //throw ex;
                return false;
            }
        }

        /// <summary>
        /// Delete Portfolio
        /// </summary>
        /// <param name="id">Portfolio id</param>
        /// <returns>true if deleted</returns>
        public static bool DeletePortfolio(long id)
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                bdd.DeleteObject(bdd.T_Portfolio.Where(x => x.IdPortfolio == id).FirstOrDefault());
                bdd.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Update the Portfolio
        /// </summary>
        /// <param name="Portfolio">the Portfolio object replacing the old one</param>
        /// <returns>true if updated</returns>
        public static bool UpdatePortfolio(T_Portfolio Portfolio)
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                T_Portfolio PortfolioDB = bdd.T_Portfolio.Where(x => x.IdPortfolio == Portfolio.IdPortfolio).FirstOrDefault();

                if (PortfolioDB != null)
                {
                    PortfolioDB.Amount = Portfolio.Amount;
                    PortfolioDB.IsActive = Portfolio.IsActive;

                    bdd.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        #endregion

        #region GetFromDBB

        /// <summary>
        /// Get a certain Portfolio
        /// </summary>
        /// <param name="id">Portfolio id</param>
        /// <returns>Portfolio object if goes well, else null</returns>
        public static T_Portfolio GetPortfolioByUserId(long id)
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                return bdd.T_Portfolio.Where(x => x.IdUser == id).FirstOrDefault();

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion
    }
}